//
//  CZComposeTextView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZComposeTextView: UITextView {

    //重写 textView 的Font属性
   override var font:UIFont?{
        didSet{
            //设置占位文字的font
            placeholderLabel.font = font
            
        }
    }
    
    //显示占位的文字
    var placeholder:String?{
        didSet{
            //设置占位的文字
            placeholderLabel.text = placeholder
        }
        
    }
    //设置显示占位文字的颜色
    var color:UIColor?{
        didSet{
            
            placeholderLabel.textColor = color
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置UI
    func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(placeholderLabel)
        //设置约束
        placeholderLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(SCREENW - 10)
        }
        
        //创建通知，监听文字的改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    //监听textView的改变
    func textViewDidChange(){
        //有值的时候就隐藏占位文字
        placeholderLabel.hidden = self.hasText()
        
    }
    
    
     //MARK: - 懒加载控件
    //设置显示的label
    private lazy var placeholderLabel:UILabel = {
        let lab = UILabel()
//        lab.textColor = UIColor.grayColor()
//        lab.text = "传智博客传智博客传智博客传智博客传智博客传智博客传智博客传智博客传智博客传智博客传智博客传智博客传智博客传智博客"
//        lab.font = UIFont.systemFontOfSize(12)
        lab.numberOfLines = 0
        
        return lab
    }()
    
    //析构函数
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
}
